package com.lelaohui.net;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.Toast;

import com.family.llh.lelaohuitoastview.dialog.toast.T;
import com.google.gson.Gson;
import com.lelaohui.net.api.Api;
import com.lelaohui.net.listener.NetDataListener;
import com.lelaohui.net.listener.ProgressListener;
import com.lelaohui.net.netmanager.INetRequestManager;
import com.lelaohui.net.netmanager.NetManager;
import com.lelaohui.net.netmanager.SystemStatusManager;
import com.lelaohui.net.request.IRequest;
import com.lelaohui.net.response.ResponseBean;
import com.lelaohui.net.utils.LLHCustomProgress;


import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by T460p on 2017/3/15.
 */

public abstract class BaseLeLaohuiAcitivity extends AppCompatActivity implements ProgressListener {
    private INetRequestManager requestManager=null;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        new SystemStatusManager(this).setTranslucentStatus(R.color.top_color);
        setContentView(getlayout());
        requestManager= getRequestManager();
        requestManager.setProgressListener(this);
        initView();
        doBasess();
    }


    protected void requestData(@NonNull final IRequest request, @NonNull final NetDataListener dataCallback){
        if (isNetworkConnected(getApplicationContext()) == false) {
            T.showAnimErrorToast(getApplicationContext(), "网络异常");
        } else {
        requestManager.postRequest(request,dataCallback);
    }
    }
    protected  void showPromot(String msg){
        Toast.makeText(this,msg,Toast.LENGTH_LONG).show();
    }
    protected abstract int getlayout();
    protected abstract void initView();
    protected abstract void doBasess();

    protected abstract INetRequestManager getRequestManager();
    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(getWindow().getAttributes().softInputMode== WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED)
        {
            //隐藏软键盘
            getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
            getWindow().getAttributes().softInputMode=WindowManager.LayoutParams.SOFT_INPUT_STATE_UNSPECIFIED;
        }
    }
    LLHCustomProgress myProgress=null;
    public void closeProgress() {
        if (myProgress == null || myProgress.isShowing()) {
            myProgress.dismiss();
        }
    }

    public void showProgress(Context context) {
        if (myProgress == null || !myProgress.isShowing()) {
            myProgress = LLHCustomProgress.show(context, "正在努力加载中.....", true,
                    null);
        }
    }
    //隐藏软键盘
    public void hideInputWindow(Activity context){
        if(context==null){
            return;
        }
        final View v = ((Activity) context).getWindow().peekDecorView();
        if (v != null && v.getWindowToken() != null) {
            InputMethodManager imm = (InputMethodManager) context.getSystemService(context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }

    /**
     * 点击他处，键盘消失
     * @param ev
     * @return
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {

                InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                if (imm != null) {
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                }
            }
            return super.dispatchTouchEvent(ev);
        }
        // 必不可少，否则所有的组件都不会有TouchEvent了
        if (getWindow().superDispatchTouchEvent(ev)) {
            return true;
        }
        return onTouchEvent(ev);
    }
    public  boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = { 0, 0 };
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }


    public boolean isNetworkConnected(Context context) {
        if (context != null) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
            if (mNetworkInfo != null) {
                return mNetworkInfo.isAvailable();
            }
        }
        return false;
    }
}
